//
//  AIGirlProfileNavView.swift
//  AIGirl
//
//  Created by Rae on 2023/12/28.
//

import UIKit

class AIGirlProfileNavView: UIView {

    private let iconImageView = c_make_ex(UIImageView.self)
        .corner(35).image("AIGirl_Default_Avatar").end()
    private let iconPreImageView = c_make_ex(UIImageView.self)
        .image("AIGirl_Profile_Avatar_VIP").mode(.scaleAspectFill).end()
    private var nameEditButton = UIIconButton(style: .trailing(itemSpace: 8))
    private let idLabel = c_make_ex(UILabel.self)
        .color("#F2F2FD".toColor.alpha(0.4)).font(14.font_regular).end()
    private let coinsButton = c_make_ex(UIButton.self)
        .image("AIGirl_Profile_coins").end()
    private lazy var bgView = c_make_ex(UIImageView.self)
        .image("AIGirl_Home_premiumBg").end()
    private lazy var diamondImageView = c_make_ex(UIImageView.self)
        .image("AIGirl_Home_preDiamond").mode(.scaleAspectFill).end()
    private lazy var titleImageView = c_make_ex(UIImageView.self)
        .image("AIGirl_Home_premium").mode(.scaleAspectFill).end()
    private lazy var checkImageView_one = c_make_ex(UIImageView.self)
        .image("AIGirl_Home_check").mode(.scaleAspectFill).end()
    private lazy var checkImageView_two = c_make_ex(UIImageView.self)
        .image("AIGirl_Home_check").mode(.scaleAspectFill).end()
    private lazy var checkImageView_three = c_make_ex(UIImageView.self)
        .image("AIGirl_Home_check").mode(.scaleAspectFill).end()
    private lazy var checkTitle_one = c_make_ex(UILabel.self)
        .text("Unlock Intimate Conversations.").color("#FBD97D")
        .font(15.font_medium).end()
    private lazy var checkTitle_two = c_make_ex(UILabel.self)
        .text("Exclusive VIP Avatar.").color("#FBD97D")
        .font(15.font_medium).end()
    private lazy var checkTitle_three = c_make_ex(UILabel.self)
        .text("Log in every day to get free messages").color("#FBD97D")
        .font(15.font_medium).end()
    private lazy var joinButton = c_make_ex(UIButton.self)
        .bgImage("AIGirl_Home_premiumBtn").corner(20)
        .text("Join Now").color("#643208").font(21.font_bold).end()
    private lazy var timeBgView = c_make_ex(UIImageView.self)
        .image("AIGirl_Home_alreadyTimeBg").end()
    private lazy var timeLabel = c_make_ex(UILabel.self)
        .color("#FFEA79")
        .font(15.font_semiItalic).end()
    
    var diamondClosure: (() -> Void)?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        subviews{
            iconImageView
            iconPreImageView
            nameEditButton
            idLabel
            coinsButton
            bgView.subviews{
                diamondImageView
                titleImageView
                checkImageView_one
                checkImageView_two
                checkImageView_three
                checkTitle_one
                checkTitle_two
                checkTitle_three
                timeBgView
                timeLabel
            }
            joinButton
        }
        
        iconImageView.leading(20).top(10.0).size(70)
        iconPreImageView.leading(16).width(74).height(87)
        iconPreImageView.Bottom == iconImageView.Bottom
        coinsButton.trailing(15).size(60)
        coinsButton.CenterY == iconImageView.CenterY
        nameEditButton.leading(105).top(20).width(<=180)
        idLabel.leading(105)
        idLabel.Top == nameEditButton.Bottom + 5
        bgView.leading(15).trailing(15).height(203)
        bgView.Top == coinsButton.Bottom + 30
        diamondImageView.leading(15).top(17).width(29).height(26)
        titleImageView.leading(45.5).top(20.5).width(109).height(18)
        checkImageView_one.leading(18).width(11).height(9)
        checkImageView_one.Top == diamondImageView.Bottom + 20.5
        checkImageView_two.leading(18).width(11).height(9)
        checkImageView_two.Top == checkImageView_one.Bottom + 17.5
        checkImageView_three.leading(18).width(11).height(9)
        checkImageView_three.Top == checkImageView_two.Bottom + 17.5
        checkTitle_one.leading(38)
        checkTitle_one.CenterY == checkImageView_one.CenterY
        checkTitle_two.leading(38)
        checkTitle_two.CenterY == checkImageView_two.CenterY
        checkTitle_three.leading(38)
        checkTitle_three.CenterY == checkImageView_three.CenterY
        joinButton.width(295).height(40).centerHorizontally()
        joinButton.Top == checkImageView_three.Bottom + 19.5
        timeBgView.trailing(0).top(20).width(185).height(25)
        timeLabel.CenterY == timeBgView.CenterY
        timeLabel.CenterX == timeBgView.CenterX
        
        
        iconPreImageView.isHidden = true
        joinButton.addTarget(self, action: #selector(clickJoinButton), for: .touchUpInside)
        coinsButton.addTarget(self, action: #selector(clickCoinButton), for: .touchUpInside)
        nameEditButton.style { v in
            v.setTitleColor(.white, for: .normal)
            v.iconSize = CGSize(width: 21, height: 21)
            v.setImage("AIGirl_Profile_edit".toImage, for: .normal)
            v.addTarget(self, action: #selector(pushToEditVC), for: .touchUpInside)
            v.titleLabel?.font = 19.font_semiBold
            v.contentHorizontalAlignment = .leading
        }
        let tapG = UITapGestureRecognizer(target: self, action: #selector(pushToAlreadyPreVC))
//        bgView.isUserInteractionEnabled = true
        bgView.addGestureRecognizer(tapG)
    }
    
    func updateUserInfo(){
        
        nameEditButton.setTitle(GlobalTool.userModel?.name, for: .normal)
        if let id = GlobalTool.userModel?.id {
            idLabel.text = "ID:" + String(id)
        }else{
            idLabel.text = ""
        }
        if let img = GlobalTool.userModel?.headImage, img.count > 0 {
            iconImageView.loadImage(url: img)
        }else{
            iconImageView.image = "AIGirl_Default_Avatar".toImage
        }
        if GlobalTool.userModel?.isVip.intValue ?? 0 > 0 {
            joinButton.isHidden = true
            bgView.heightConstraint?.constant = 155
            iconPreImageView.isHidden = false
            timeBgView.isHidden = false
            timeLabel.isHidden = false
            bgView.isUserInteractionEnabled = true
            let startT = UserDefaultsManager.getString(forKey: .VIPStartTime) ?? ""
            let endT = UserDefaultsManager.getString(forKey: .VIPEndTime) ?? ""
            if startT.count > 0 && endT.count > 0 {
                timeLabel.text = startT + "-" + endT
            }
        }else{
            joinButton.isHidden = false
            bgView.heightConstraint?.constant = 203
            iconPreImageView.isHidden = true
            timeBgView.isHidden = true
            timeLabel.isHidden = true
            bgView.isUserInteractionEnabled = false
        }
    }
    
    @objc private func pushToAlreadyPreVC(){
        GlobalTool.showAlreadyVipInfoPage()
    }
    
    @objc private func clickCoinButton(){
        diamondClosure?()
    }
    
    @objc private func clickJoinButton(){
        GlobalTool.showVipInfoPage()
    }
    
    @objc private func pushToEditVC(){
        
        UIApplication.topController()?.navigationController?.pushViewController(AIGirlProfileEditViewController(), animated: true)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class AIGirlProfileHeaderView: UITableViewHeaderFooterView {

    override init(reuseIdentifier: String?) {
        super.init(reuseIdentifier: reuseIdentifier)
        backgroundColor = "#262C61".toColor
        contentView.backgroundColor = "#262C61".toColor
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
